﻿using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices.AccountManagement;

namespace ICeFramework.Common.ActiveDirectory
{
    [Serializable]
    public class UserInfo : ADObject
    {
        private string _locationCode;
        public string LocationCode
        {
            get { return _locationCode; }
            set { _locationCode = value; }
        }

        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

       
        private string _emailAddress;
        public string EmailAddress
        {
            get { return _emailAddress; }
            set { _emailAddress = value; }
        }

        private bool _accountDisabled;
        public bool AccountDisabled
        {
            get { return _accountDisabled; }
            set { _accountDisabled = value; }
        }

        public override ADObjectType ObjectType
        {
            get { return ADObjectType.User; }
        }

        public UserInfo()
            : this(string.Empty, ADContextType.Unknown, null)
        {

        }

        public UserInfo(string accountName, ADContextType contextType, UserPrincipal userPrincipal)
            : base(accountName, contextType, userPrincipal)
        {
        }

        public static UserInfo GetUserInfo(UserPrincipal userPrincipal, ADContextType userContextType)
        {
            if (userPrincipal == null)
            {
                return null;
            }

            UserInfo info = new UserInfo(userPrincipal.SamAccountName, userContextType, userPrincipal);
            info.AccountDisabled = userPrincipal.Enabled.HasValue ? !userPrincipal.Enabled.Value : false;
            info.EmailAddress = userPrincipal.EmailAddress;
            info.FirstName = userPrincipal.Name;
            info.LastName = userPrincipal.Surname;
            info.LocationCode = userPrincipal.Context.Name;
            info.DisplayName = string.IsNullOrEmpty(userPrincipal.DisplayName) ? userPrincipal.Name : userPrincipal.DisplayName;

            return info;
        }
    }
}
